﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BAL.PublishSharepoint;
using System.IO;
using System.Data;

//namespace BAL.EstRec.FDVUtil
namespace BAL.EstRec.FDVUndoPublish
{
    public class UndoPublishFDV
    {
        string unidVersion;
        string localPubPath;
        string sharedPubPath;
        List<string> recent;

        public UndoPublishFDV(string unid, AppConfs cnfs)
        {
            this.unidVersion = unid;
            this.sharedPubPath = cnfs.cnf.SharedFolderFDV;
            this.localPubPath = cnfs.cnf.PublicarRHQ;
        }//

        public void UndoFDVPublish()
        {
            try
            {
                //1. Obtener lista de los más recientes por versión en la carpeta local
                this.getRecentFiles();

                //2. Borrar archivos listados en carpeta local
                if (recent.Count == 0)
                    return;

                this.deleteFromFolder(this.localPubPath);

                //3. Borrar de sharedFolder
                this.deleteFromFolder(this.sharedPubPath);


                //4. Borrar de Workpoint
                this.deleteFromWorkPoint();

                //5. Actualizar FDV_BGT_VERSION
                this.DeleteDatePublishFDV();

            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                throw ee;
            }
            
        }

        private void deleteFromFolder(string folder)
        {
            foreach (string file in this.recent)
            {
                string f = folder + file +".xlsx";

                if(File.Exists(f))
                    File.Delete(f);
            }
        
        }//

        private void deleteFromWorkPoint()
        {
            System.Data.DataTable dt = this.GetPathsSP();    //Obtiene las rutas de publicación en workpoint desde el catálogo de Recurso

            if (dt.Rows.Count > 0)
            {
                foreach (System.Data.DataRow item in dt.Rows)
                {
                    try
                    {
                        string lib = item["PATH_PUBLICACION"].ToString().Split('/').Last();
                        string contexto = item["PATH_PUBLICACION"].ToString().Replace(lib, "");

                        PublishSP sp = new PublishSP(contexto, lib, this.unidVersion.ToString(), this.recent);
                        sp.DeleteFilesFromWorkpoint();

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        
                    }
                }
            }//if
        }//

        private void getRecentFiles()
        {
            List<string> arr = new List<string>();

            foreach (string f in Directory.GetFiles(this.localPubPath))
            {
                if(f.Split('\\').Last().Split('_')[0] ==this.unidVersion)
                    arr.Add(f.Split('\\').Last() );
            }

            this.recent =  ConsFileInfo.GetLatest(arr);

            
        
        }//

        private System.Data.DataTable GetPathsSP()
        {
            string query = "SP_ER_GET_SPPATHS";
            System.Data.DataTable dt = new System.Data.DataTable();
            try
            {
                dt = DAL.EstRec.SrvDB.ExecuteQuery(query);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                
            }
            return dt;
        }//

        private void DeleteDatePublishFDV()
        {

            string query = "SP_ER_DEL_FDVDATEPUBLISH @unidVersion= " + this.unidVersion;

            try
            {
                DAL.EstRec.SrvDB.ExecuteNonQuery(query);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                
            }
        
        }//

        public bool isPublish()
        {
            string query = "SP_ER_GET_ISVERSIONPUBLISHED @UNID_VERSION = "+this.unidVersion;
            bool isPublished = false;
            try
            {
                DataTable dt = DAL.EstRec.SrvDB.ExecuteQuery(query);
                if (dt.Rows.Count > 0)
                    isPublished = Convert.ToBoolean(dt.Rows[0]["IS_PUBLISHED"].ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return isPublished;
        }//

    }//end class
}
